const app = getApp()
 
const request = (url, options) => {
   return new Promise((resolve, reject) => {
    let value = wx.getStorageSync('token')
    let headurl=`https://yunsrm.hien.cn/api/`
    wx.setStorageSync('headurl', headurl)
       wx.request({
            // {app.globalData.host}为接口请求中的公共部分写在app.js中
           url: headurl+url,
           method: options.method,
           data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
           header: {
               'Content-Type': 'application/json; charset=UTF-8',
                'token': value
           },
           success(request) {
               if (request.data.code === 200) {
                   resolve(request.data)
               } 
               else if(request.data.code === 500) {
                 wx.showToast({
                  icon:'none',
                  title: request.data.message,
                  duration: 2000
                })
               }
               else if(request.data.code === 404) {
                wx.showToast({
                 icon:'none',
                 title: "连接出错",
                 duration: 2000
               })
              }
              else{
                wx.showToast({
                  icon:'none',
                  title: request.data.message,
                  duration: 2000
                })
              }
           },
           fail(error) {
               reject(error.data)
           }
       })
   })
}
 
const get = (url, options = {}) => {
   return request(url, { method: 'GET', data: options })
}
const post = (url, options) => {
   return request(url, { method: 'POST', data: options })
}
const put = (url, options) => {
   return request(url, { method: 'PUT', data: options })
}
// 不能声明DELETE（关键字）
const remove = (url, options) => {
   return request(url, { method: 'DELETE', data: options })
}
 
module.exports = {
   get,
   post,
   put,
   remove
}